/*
 * Copyright 2016 czy1121
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ezy.boost.update;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class UpdateInfo {
    // 是否有新版本
    public boolean hasUpdate = false;
    // 是否静默下载：有新版本时不提示直接下载
    public boolean isSilent = false;
    // 是否强制安装：不安装无法使用app
    public boolean isForce = false;
    // 是否下载完成后自动安装
    public boolean isAutoInstall = true;

    // 是否可忽略该版本
    public boolean isIgnorable = false;
    // 一天内最大提示次数，<1时不限
    public int maxTimes = 1;

    //    public int versionCode;
//    public String versionName;
//
    public String updateContent;
    //
    public String url;
    public String md5;
//    public long size;

    public String name;
    public String version;
    // public int is_ft_up;

    public static UpdateInfo parse(String s) throws JSONException {
        JSONObject o = new JSONObject(s);
        return parse(o.has("data") ? o.getJSONObject("data") : o);
    }

    private static UpdateInfo parse(JSONObject o) {
        UpdateInfo info = new UpdateInfo();
        if (o == null) {
            return info;
        }
        LogUtils.i("营维189 更新 :" + o.toString());

        String versionLoc = AppUtils.getAppVersionName();

        boolean isNew = false;
        try {
            String version = o.optString("version").replaceAll("\\.", "");
            versionLoc = versionLoc.replaceAll("\\.", "");
            //线上大  本地小  true     线上小  本地大  false
            int newVersionlength = version.length();
            int locVersionLength = versionLoc.length();
            int newLength = (newVersionlength > locVersionLength) ? newVersionlength : locVersionLength;
            if (newLength > newVersionlength) {
                int moreLength = newLength - newVersionlength;
                for(int index = 0 ; index < moreLength ; index ++){
                    version += "0";
                }
            } else if (newLength > locVersionLength) {
                int moreLength = newLength - locVersionLength;
                for(int index = 0 ; index < moreLength ; index ++){
                    versionLoc += "0";
                }
            }
            isNew = Integer.parseInt(version) > Integer.parseInt(versionLoc);
        } catch (Exception e) {
            LogUtils.e("营维189 " + e);
        }

        if (isNew) {
            info.hasUpdate = true;
        } else {
            return info;
        }

        info.name = o.optString("name");

        info.version = o.optString("version");

        info.url = o.optString("path");

        JSONArray listArr = o.optJSONArray("list");

        StringBuilder strContent = new StringBuilder();
        for (int i = 0; i < listArr.length(); i++) {
            strContent.append(listArr.optString(i) + "\r\n");
        }
        info.updateContent = strContent.toString();

        LogUtils.i("营维189 更新:" + info.name + info.version);
        info.md5 = EncryptUtils.encryptHmacMD5ToString(info.version, "yingwei189");

        int is_ft_up = o.optInt("is_ft_up", 1);
        if (is_ft_up == 2) {
            info.isForce = true;
        }
        info.isIgnorable = false;
        //   info.is_ft_up = o.optInt("is_ft_up", 0);

        return info;
    }

    @Override
    public String toString() {
        return "UpdateInfo{" +
                "hasUpdate=" + hasUpdate +
                ", isSilent=" + isSilent +
                ", isForce=" + isForce +
                ", isAutoInstall=" + isAutoInstall +
                ", isIgnorable=" + isIgnorable +
                ", maxTimes=" + maxTimes +
                ", updateContent='" + updateContent + '\'' +
                ", url='" + url + '\'' +
                ", md5='" + md5 + '\'' +
                ", name='" + name + '\'' +
                ", version='" + version + '\'' +
                ", isForce=" + isForce +
                '}';
    }
}